package com.jcl.core.web.wapper;

import com.jcl.core.web.utils.XssUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Map;

/**
 * @ClassName XssHttpServletRequestWapper

 * @Author yyliu
 * @Date 2018/9/19 15:23
 * @Version 1.0
 **/
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {

    public XssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getParameter(String name) {
        String param = super.getParameter(name);
        if (null == param) {
            return null;
        }
        return XssUtils.xssClean(param);
    }

    @Override
    public String getHeader(String name) {
        String header = super.getHeader(name);
        if (null == header) {
            return null;
        }
        return XssUtils.xssClean(header);
    }

    @Override
    public Map<String, String[]> getParameterMap() {

        Map<String, String[]> paramMap = super.getParameterMap();

        if (null != paramMap && paramMap.size() > 0) {
            for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
                String[] arr = entry.getValue();
                for (int i = 0, len = arr.length; i < len; i++) {
                    arr[i] = XssUtils.xssClean(arr[i]);
                }
            }
        }

        return paramMap;
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] arr = super.getParameterValues(name);
        if (null != arr) {
            for (int i = 0, len = arr.length; i < len; i++) {
                arr[i] = XssUtils.xssClean(arr[i]);
            }
        }

        return arr;
    }

}
